local time = 0;
local armortypes = {0.5, 0.66, 0.75};

MgHUD_Health = 
{
    userData = {}
}
registerWidget("MgHUD_Health")

--[[
function MgHUD_Health:initialize(x, y, intensity)
    self.userData = loadUserData()
    CheckSetDefaultValue(self, "userData", "table", {})
    CheckSetDefaultValue(self.userData, "draw", "boolean", true)
end

function MgHUD_Health:drawOptions(x, y, intensity)
    local optargs = {}
    optargs.intensity = intensity
    local user = self.userData

    user.test = ui2RowCheckbox(x, y, WIDGET_PROPERTIES_COL_INDENT, "test item", user.test, optargs)

    y = y + 50

    user.draw = ui2RowCheckbox(x, y, WIDGET_PROPERTIES_COL_INDENT,
                               "draw this I guess", user.draw, optargs)

end
]]--

function MgHUD_Health:draw()
 
    -- Early out if HUD shouldn't be shown.
    if not shouldShowHUD() then return end
	if isRaceMode() then return end

    ------ INCR TIME ------
    time = time + deltaTime;

    ------ GET PLAYER ------
    local player = getPlayer();

    ------ OPTIONS ------
    local enableHealthColors = false;
    local enableBox = true;
    local healthColor = Color(255,255,255);
    local lowHealth = 60;
    local warnOnFatalDamage = false;
    local fatalDamageValue = 100; -- Default 100, the damage of a direct rocket.

    ------ HELPERS ------
    local health = player.health;
    local armor = player.armor;
    local atype = player.armorProtection;
    local boxPadding = 30;

    ------ HEALTH COLORS ------

    if enableHealthColors then
        if health > 80 then
            healthColor = Color(255,255,255);
        elseif health > 50 then
            healthColor = Color(255,176,14);
        else
            healthColor = Color(236,0,0);
        end
    end

    ------ LOW/HIGH-HP BOX ------
    local boxLength = 0
    -- 48px:[1]
    -- 77px:[023456789]
    
    boxLength = boxLength + 77
    if (health > 9) then boxLength = boxLength + 77 end
    if (health > 99) then boxLength = boxLength + 77 end

    if not warnOnFatalDamage then
        if (health < lowHealth) then 
            drawLowHealthWarning(boxPadding, boxLength) 
        end
    else
        if isDamageFatal(fatalDamageValue, health, armor, atype) then 
            drawLowHealthWarning(boxPadding, boxLength) 
        end
    end
    
    if (health > 100) then
        drawOverhealBox(boxPadding, boxLength)
    end

    ------ DROP-SHADOW COLOR ------
    local shadowColor;
    if player.hasMega then
        shadowColor = Color(17,116,217);
    else
        shadowColor = Color(48,48,48);
    end
    
    ------ DRAW TEXT ------
    nvgFontSize(130);
    nvgFontFace("CodeProLC");
    nvgTextAlign(NVG_ALIGN_RIGHT, NVG_ALIGN_TOP);

    nvgFillColor(shadowColor);
    nvgText(4, 2, health);
    nvgFillColor(healthColor);
    nvgText(0, 0, health);
end

-- Returns true if a hit of {dmg} damage will kill the player in 1 hit.
function isDamageFatal(dmg, hp, armor, atype)
    local mult = armortypes[atype+1]; -- I hate Lua.
    local d = dmg - (dmg * mult);
    local armorDmg = dmg - d;
    if armor < armorDmg then
        d = d + (armorDmg - armor)
    end
    return hp <= d
end

function drawLowHealthWarning(boxPadding, boxLength)
    local alpha = 60
    if time%0.4>0.2 then alpha = 160 end
    nvgBeginPath();
    nvgFillColor(Color(200,0,0,alpha));
    nvgRect(boxPadding/2,-3,-boxLength - boxPadding,108);
    nvgFill()
end

function drawOverhealBox(boxPadding, boxLength)
    local alpha = 30;
    local pulsePower = 100
    local pulseTime = math.floor((time%1.5/1.5)*pulsePower)
    if pulseTime < pulsePower/2 then
        alpha = alpha + pulseTime
    elseif pulseTime >= pulsePower/2 then
        alpha = alpha + pulsePower-pulseTime
    end
    nvgBeginPath();
    nvgFillColor(Color(0,200,100,alpha));
    nvgRect(boxPadding/2,-3,-220 - boxPadding,108);
    nvgFill()
end
